package com.yuanqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.entity.Inventory;
import com.yuanqi.service.InventoryService;
import com.yuanqi.mapper.InventoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Date;

/**
* @author HP
* @description 针对表【inventory(库存余额)】的数据库操作Service实现
* @createDate 2025-08-27 11:07:31
*/
@Slf4j
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory>
    implements InventoryService{

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean decreaseInventory(Long warehouseId, Long skuId, BigDecimal qty, String batchNo) {
        try {
            // 查找库存记录
            LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Inventory::getWarehouseId, warehouseId)
                       .eq(Inventory::getSkuId, skuId)
                       .eq(Inventory::getBatchNo, batchNo);
            
            Inventory inventory = this.getOne(queryWrapper);
            if (inventory == null) {
                log.error("库存记录不存在: warehouseId={}, skuId={}, batchNo={}", warehouseId, skuId, batchNo);
                return false;
            }
            
            // 检查库存是否足够
            if (inventory.getAvailableQty().compareTo(qty) < 0) {
                log.error("库存不足: 可用数量={}, 需要数量={}", inventory.getAvailableQty(), qty);
                return false;
            }
            
            // 更新库存
            LambdaUpdateWrapper<Inventory> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Inventory::getId, inventory.getId())
                        .set(Inventory::getOnHandQty, inventory.getOnHandQty().subtract(qty))
                        .set(Inventory::getAvailableQty, inventory.getAvailableQty().subtract(qty))
                        .set(Inventory::getUpdatedAt, new Date());
            
            boolean updated = this.update(updateWrapper);
            if (updated) {
                log.info("库存减少成功: warehouseId={}, skuId={}, batchNo={}, qty={}", 
                        warehouseId, skuId, batchNo, qty);
            }
            
            return updated;
        } catch (Exception e) {
            log.error("减少库存失败: {}", e.getMessage(), e);
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean increaseInventory(Long warehouseId, Long skuId, BigDecimal qty, String batchNo, Date expiryDate) {
        try {
            // 查找库存记录
            LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Inventory::getWarehouseId, warehouseId)
                       .eq(Inventory::getSkuId, skuId)
                       .eq(Inventory::getBatchNo, batchNo);
            
            Inventory inventory = this.getOne(queryWrapper);
            
            if (inventory == null) {
                // 创建新的库存记录
                inventory = new Inventory();
                inventory.setWarehouseId(warehouseId);
                inventory.setLocationId(1L); // 设置默认库位ID为1
                inventory.setSkuId(skuId);
                inventory.setBatchNo(batchNo);
                inventory.setExpiryDate(expiryDate);
                inventory.setOnHandQty(qty);
                inventory.setAvailableQty(qty);
                inventory.setOccupiedQty(BigDecimal.ZERO);
                inventory.setUpdatedAt(new Date());
                
                boolean saved = this.save(inventory);
                if (saved) {
                    log.info("创建库存记录成功: warehouseId={}, skuId={}, batchNo={}, qty={}", 
                            warehouseId, skuId, batchNo, qty);
                }
                return saved;
            } else {
                // 更新现有库存记录
                LambdaUpdateWrapper<Inventory> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(Inventory::getId, inventory.getId())
                            .set(Inventory::getOnHandQty, inventory.getOnHandQty().add(qty))
                            .set(Inventory::getAvailableQty, inventory.getAvailableQty().add(qty))
                            .set(Inventory::getUpdatedAt, new Date());
                
                if (expiryDate != null) {
                    updateWrapper.set(Inventory::getExpiryDate, expiryDate);
                }
                
                boolean updated = this.update(updateWrapper);
                if (updated) {
                    log.info("库存增加成功: warehouseId={}, skuId={}, batchNo={}, qty={}", 
                            warehouseId, skuId, batchNo, qty);
                }
                return updated;
            }
        } catch (Exception e) {
            log.error("增加库存失败: {}", e.getMessage(), e);
            throw e;
        }
    }
}




